package by.dvrudenko.avtobaza.webapp.page.createUser;

import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.User;
import by.dvrudenko.avtobaza.datamodel.enums.UserRole;
import by.dvrudenko.avtobaza.services.UserService;
import by.dvrudenko.avtobaza.webapp.page.BaseLayout;
import by.dvrudenko.avtobaza.webapp.page.home.HomePage;
import by.dvrudenko.avtobaza.webapp.utils.validators.LoginValidator;


public class CreateUserPage extends BaseLayout {

	@Inject
	private UserService userService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CreateUserPage.class);
	
	// создаем нового пользователя
	public CreateUserPage(final User user, UserRole userRole) {
		super();
		user.setRole(userRole);
		final List <User> listUser = userService.getAllUser();
		Form<User> formCreateUser = new Form<User>("form", new CompoundPropertyModel<User>(user));
		
		final TextField<String> userName = new TextField<String>("name");
		userName.add(new PropertyValidator<String>());
		userName.setLabel(new ResourceModel("p.home.createedit.user.name"));
		formCreateUser.add(userName);
		
		final TextField<String> userLogin = new TextField<String>("login");
		userLogin.add(new PropertyValidator<String>());	
		userLogin.setLabel(new ResourceModel("p.home.createedit.user.login"));
		userLogin.add(new LoginValidator(listUser));	
		formCreateUser.add(userLogin);
		
		final TextField<String> userPassword = new TextField<String>("password");	
		userPassword.add(new PropertyValidator<String>());
		userPassword.setLabel(new ResourceModel("p.home.createedit.user.password"));
		formCreateUser.add(userPassword);
		
		final TextField<String> userEmail = new TextField<String>("email");
		userEmail.add(new PropertyValidator<String>());
		userEmail.add(EmailAddressValidator.getInstance());
		userEmail.setLabel(new ResourceModel("p.home.createedit.user.email"));	
		formCreateUser.add(userEmail);
		
		/*final TextField<String> userLogo = new TextField<String>("logo");
		formCreateUser.add(userLogo);*/
		
				
		formCreateUser.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				// устанавливаем роль USER
				user.setRole(UserRole.USER);
				userService.saveOrUpdate(user);
				LOGGER.info("create new user " + user.getLogin());
				HomePage homePage = new HomePage(); 
				homePage.info(new ResourceModel("p.home.createedit.user.logoSuccess").getObject());
				setResponsePage(homePage);
			}
		});
		
		add(formCreateUser);
	}
	
}
